package controller;

import javax.annotation.Resource;
import javax.servlet.http.HttpServletRequest;

import java.io.File;
import java.util.*;
import org.springframework.stereotype.Controller;
import org.springframework.web.bind.annotation.RequestMapping;
import org.springframework.web.multipart.MultipartFile;

import util.BaseDAO;

import dao.*;
import entity.*;
import java.util.*;

@Controller
@RequestMapping("/report")
public class ReportController extends BaseController {
	BaseDAO dao = new BaseDAO();

	@RequestMapping("/show")
	public String show(HttpServletRequest request) {
		  String one = "select usersid,count(*) from yudingji group by usersid order by count(*) desc";
		  String two = "select yudingji.usersid,sum(feiji.distance) from yudingji,feiji where yudingji.feijiid=feiji.id group by yudingji.usersid order by sum(feiji.distance) desc";
		  
		  String result = "select yudingji.usersid,count(*),sum(feiji.distance),count(*) * sum(feiji.distance) from yudingji,feiji where yudingji.feijiid=feiji.id group by yudingji.usersid order by count(*) desc,count(*) * sum(feiji.distance) desc";
		  List<Object[]> message = dao.find(result, null);
		  if(message!=null && message.size()>0){
			  for(Object[] data : message){
				  int userId = (Integer)data[0];
				  int twice = (Integer)data[1];
				  int distance = (Integer)data[3];
				  
				  dao.operate("delete from level where usersId=?",new Object[]{userId});
				  
				  Object[] account = dao.findSingle("select * from users where id="+userId,null);
				  dao.operate("insert into level values(?,?,?,?,?,?,?,?)", new Object[]{userId,account[1],account[3],account[8],account[6],account[9],twice,distance});
				  
			  }
		 }
		 List all = dao.find("select * from level", null);
		 request.setAttribute("all", all);
		 return "admin/report";
	}
}
